package com.gzzq.service;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import com.gzzq.entity.QueueVO;

@Service("producer")
public class Producer {
	@Autowired // 也可以注入JmsTemplate，JmsMessagingTemplate对JmsTemplate进行了封装
	private JmsTemplate jmsTemplate;
	// 发送消息，destination是发送到的队列，message是待发送的消息
	public void sendMessage(String QueueName, final QueueVO vo){
		jmsTemplate.setPriority(vo.getPriority());
		jmsTemplate.send(QueueName, new MessageCreator() {
			@Override
			public Message createMessage(Session session) throws JMSException {
				ObjectMessage objectMessage = session.createObjectMessage(vo.getMsg());
				objectMessage.setJMSPriority(vo.getPriority());
				return objectMessage;
			}
	});
}
}